home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 343_02 / ht.c < prev    next >
C/C++ Source or Header  |  1992-02-27  |  16KB  |  591 lines

  1.  
  2.  
  3.        /**************************************************
  4.        *
  5.        *       file d:\cips\ht.c
  6.        *
  7.        *       Functions: This file contains
  8.        *           display_using_halftoning
  9.        *           half_tone
  10.        *           show_half_tone
  11.        *           get_threshold_value
  12.        *
  13.        *       Purpose: This program displays an image using a half
  14.        *          toning process.  The algorithm was taken from
  15.        *          "Personal computer based image processing with
  16.        *          halftoning," John A Saghri, Hsieh S. Hou, Andrew
  17.        *          Tescher, Optical Engineering, March 1986, Vol.25,
  18.        *          No. 3, pp 499-503. The display_using_halftoning
  19.        *          determines display size and reads the image.
  20.        *          The half_tone function implements the algorithm
  21.        *          shown on page 502 of the article.
  22.        *          
  23.        *          The function print_halftone_array prints a half
  24.        *          toned image array to a regular line printer.
  25.        *
  26.        *
  27.        *       External Calls:
  28.        *          rtiff.c - read_tiff_image
  29.        *          rstring.c - read_string
  30.        *          numcvrt.c - get_integer
  31.        *
  32.        *       Modifications:
  33.        *          30 September 86 - created
  34.        *          18 August 1990 - modified for use in the
  35.        *              C Image Processing System.
  36.        *
  37.        **************************************************/
  38.  
  39.  
  40. #include "d:\cips\cips.h"
  41.  
  42. #define  FORMFEED  '\014'
  43.  
  44. float eg[ROWS][COLS], ep[ROWS][COLS];
  45.  
  46. display_using_halftoning(in_image, file_name,
  47.          il, ie, ll, le, threshold, invert,
  48.          image_colors, image_header, monitor_type,
  49.          print, show_hist, color_transform)
  50.  
  51.    char  color_transform[], file_name[], monitor_type[];
  52.    int   image_colors, invert,
  53.          il, ie, ll, le, threshold,
  54.          print, show_hist;
  55.    short in_image[ROWS][COLS];
  56.    struct tiff_header_struct *image_header;
  57.  
  58. {
  59.    char response[80];
  60.  
  61.    int  a,
  62.         b,
  63.         channel,
  64.         color,
  65.         count,
  66.         data_color,
  67.         display_mode,
  68.         horizontal,
  69.         i,
  70.         j,
  71.         k,
  72.         l,
  73.         line_color,
  74.         max_horizontal,
  75.         max_vertical,
  76.         not_finished,
  77.         one,
  78.         vertical,
  79.         x,
  80.         x_offset,
  81.         y,
  82.         y_offset,
  83.         zero;
  84.  
  85.    float area, new_grays;
  86.  
  87.    unsigned long histogram[256], new_hist[256];
  88.  
  89.  
  90.        if(  (show_hist == 1)  &&
  91.             (color_transform[0] != 'H'))
  92.           zero_histogram(histogram);
  93.  
  94.          /*******************************************
  95.          *
  96.          *   Use the monitor type to set the vertical
  97.          *   horizontal and display_mode parameters.
  98.          *   Also set the values for one and zero.
  99.          *   one and zero will vary depending on the
  100.          *   monitor type.
  101.          *
  102.          ********************************************/
  103.  
  104.  
  105.       if(  (monitor_type[0] == 'M')  ||
  106.            (monitor_type[0] == 'm')){
  107.          vertical     = 3;
  108.          horizontal   = 2;
  109.          display_mode = _HRESBW;
  110.          one          = 1;
  111.          zero         = 0;
  112.       }
  113.  
  114.       if(  (monitor_type[0] == 'C')  ||
  115.            (monitor_type[0] == 'c')){
  116.          vertical     = 3;
  117.          horizontal   = 2;
  118.          display_mode = _MRES4COLOR;
  119.          one          = 3;
  120.          zero         = 1;
  121.       }
  122.  
  123.       if(  (monitor_type[0] == 'V')  ||
  124.            (monitor_type[0] == 'v')){
  125.          vertical     = 6;
  126.          horizontal   = 4;
  127.          display_mode = _VRES16COLOR;
  128.          one          = 5;
  129.          zero         = 1;
  130.       }
  131.  
  132.       if(  (monitor_type[0] == 'E')  ||
  133.            (monitor_type[0] == 'e')){
  134.          vertical     = 6;
  135.          horizontal   = 3;
  136.          display_mode = _ERESCOLOR;
  137.          one          = 5;
  138.          zero         = 1;
  139.       }
  140.  
  141.       max_horizontal = (image_header->image_length+50)/COLS;
  142.       max_vertical   = (image_header->image_width+50)/ROWS;
  143.  
  144.       if(horizontal > max_horizontal) horizontal = max_horizontal;
  145.       if(vertical > max_vertical) vertical = max_vertical;
  146.  
  147.       if(print == 1){
  148.          vertical   = 1;
  149.          horizontal = 1;
  150.       }
  151.  
  152.  
  153.  
  154.         /****************************************
  155.         *
  156.         *   If color transform wants histogram
  157.         *   equalization, then read in the
  158.         *   image arrays and calculate the
  159.         *   histogram.   Zero both the histogram
  160.         *   and the new_hist.  You will need the
  161.         *   new_hist if you want to display the
  162.         *   equalized hist.
  163.         *
  164.         *****************************************/
  165.  
  166.       if(color_transform[0] == 'H'){
  167.          count = 1;
  168.          zero_histogram(histogram);
  169.          zero_histogram(new_hist);
  170.          for(a=0; a<vertical; a++){
  171.             for(b=0; b<horizontal; b++){
  172.  
  173.                x = a*COLS;
  174.                y = b*ROWS;
  175.  
  176.                printf("\nHT> Calculating histogram");
  177.                printf(" %d of %d",count,vertical*horizontal);
  178.                count++;
  179.  
  180.                read_tiff_image(file_name, in_image, il+y,
  181.                             ie+x, ll+y, le+x);
  182.                calculate_histogram(in_image, histogram);
  183.  
  184.             }  /* ends loop over b */
  185.          }  /* ends loop over a */
  186.       }  /* ends if display_mode == H */
  187.  
  188.  
  189.  
  190.  
  191.            /* set graphics mode */
  192.       if(print == 0)
  193.          _setvideomode(display_mode); /* MSC 6.0 */
  194.       else{
  195.          printf("\n\nHT> Calculating for printing ");
  196.          printf("\nHT> Counting from 0 to 99\n");
  197.       }
  198.  
  199.         /*********************************************
  200.         *
  201.         *   Loop over horizontal and vertical. Read
  202.         *   the image array and display it after
  203.         *   calculating the half tone values.
  204.         *
  205.         *
  206.         *   If you want to show the histogram AND
  207.         *   do not want to do hist equalization
  208.         *   then calculate the hist from the 
  209.         *   original image array.
  210.         *
  211.         *   If you want to do hist equalization
  212.         *   then calculate the new_hist AFTER
  213.         *   the image has been equalized.
  214.         *
  215.         *   We will equalize the histogram down
  216.         *   to half the original shades of gray
  217.         *   and will cut the threshold in half.
  218.         *
  219.         *****************************************/
  220.  
  221.  
  222.  
  223.       for(i=0; i<horizontal; i++){
  224.          for(j=0; j<vertical; j++){
  225.             read_tiff_image(file_name, in_image, il+i*ROWS,
  226.                            ie+j*COLS, ll+i*ROWS, le+j*COLS);
  227.  
  228.             if(   (show_hist == 1)  &&
  229.                   (color_transform[0] != 'H'))
  230.                calculate_histogram(in_image, histogram);
  231.  
  232.             if(color_transform[0] == 'H'){
  233.  
  234.                area = ((long)(vertical))*((long)(horizontal));
  235.                area = area*10000.0;
  236.                new_grays = image_colors/2;
  237.  
  238.                perform_histogram_equalization(in_image,
  239.                         histogram, new_grays, area);
  240.  
  241.                calculate_histogram(in_image, new_hist);
  242.  
  243.             }  /* ends if color_transform == S */
  244.  
  245.             if(color_transform[0] == 'H')
  246.                half_tone(in_image, threshold/2, eg, ep, i, j,
  247.                       one, zero, invert, print);
  248.  
  249.             else
  250.                half_tone(in_image, threshold, eg, ep, i, j,
  251.                       one, zero, invert, print);
  252.  
  253.          }  /* ends loop over j */
  254.       }  /*  ends loop over i */
  255.  
  256.  
  257.  
  258.          /***************************
  259.          *
  260.          *   if show_hist == 1 then
  261.          *   display the histogram
  262.          *   in the lower right hand
  263.          *   corner of screen
  264.          *
  265.          ****************************/
  266.  
  267.       if(  (show_hist == 1)   &&
  268.            (print == 0)){
  269.  
  270.          if(monitor_type[0] == 'V'){
  271.             y_offset   = 470;
  272.             x_offset   = 380;
  273.             line_color = 3;
  274.             data_color = 8;
  275.          }
  276.  
  277.          if(monitor_type[0] == 'E'){
  278.